package com.lagou.task20;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.TypeVariable;
import java.sql.Array;
import java.util.Arrays;

public class PersonConstructorTest {
    public static void main(String[] args) throws Exception {
        //无参数方式创建对象的两种方法
        //1.使用无参数构造方法创建对象
        Person person = new Person();
        System.out.println("person = " + person.toString());

        //2.使用反射机制以无参数形式构造Person的对象
        Class c1 = Class.forName("com.lagou.task20.Person");
        Object person1 = c1.newInstance();
        System.out.println("person1 = " + person1.toString());

        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
        String className = br.readLine();
        Class c2 = Class.forName(className);
        Object person2 = c1.newInstance();
        System.out.println("person2 = " + person2.toString());
        br.close();

        System.out.println("----------------constructor---------------");
        Constructor constructor = c1.getConstructor();
        Constructor constructor1 = c1.getConstructor(String.class, int.class);
        Constructor[] constructors = c1.getConstructors();
        System.out.println("无参数方式构造对像"+constructor.newInstance());
        System.out.println("有参数方式构造对像"+constructor1.newInstance("liyulin", 30));
        System.out.println(Arrays.toString(constructors));

        System.out.println("----------------------------------------------------------");
        for(Constructor ct : constructors){
            //获取构造方法名
            System.out.println("构造方法名"+ct.getName());
            //获取修饰符
            System.out.println("构造方法名修饰符"+ct.getModifiers());
            //获取参数
            Class[] parameterTypes = ct.getParameterTypes();
            for (int i = 0; i < parameterTypes.length; i++) {
                System.out.println("构造方法参数类型"+parameterTypes[i]);
            }

            System.out.println("-------------------------------------------");
        }

    }
}
